package models

type RegionalFoods struct {
	RegionalFoodID     uint64 `gorm:"column:regional_food_id;primary_key;auto_increment" json:"regional_food_id"`
	RegionName         string `gorm:"column:region_name;not null" json:"region_name" `
	FoodDescription    string `gorm:"column:food_description;not null" json:"food_description"`
	CulturalBackground string `gorm:"column:cultural_background;not null" json:"cultural_background" `
	RecipeID           uint64 `gorm:"column:recipe_id;not null" json:"recipe_id" `
}

func (regionFood *RegionalFoods) Delete() error {
	return DB.Self.Delete(&regionFood).Error
}
func (regionFood *RegionalFoods) Create() error {
	return DB.Self.Create(&regionFood).Error
}

func (regionFood *RegionalFoods) Update() error {
	return DB.Self.Model(&regionFood).Update(&regionFood).Error
}

// 根据Id查询
func GetRegionalFoodsById(id uint64) (*RegionalFoods, error) {
	regionFood := &RegionalFoods{}
	r := DB.Self.First(regionFood, id)
	return regionFood, r.Error
}

// 查询食谱对应地方美食
func GetRegionByRecipeId(recipeId uint64) ([]*RegionalFoods, error) {
	regionFood := make([]*RegionalFoods, 0)
	err := DB.Self.Where("recipe_id = ?", recipeId).Find(&regionFood).Error
	return regionFood, err
}

// 查询所有地域美食
func GetRegion() ([]*RegionalFoods, error) {
	regionFood := make([]*RegionalFoods, 0)
	err := DB.Self.Find(&regionFood).Error
	return regionFood, err
}

// 模糊查询地域美食
func GetRegionByKey(key string) ([]*RegionalFoods, error) {
	regionList := make([]*RegionalFoods, 0)
	err := DB.Self.Where("region_name like ? ", "%"+key+"%").Find(&regionList).Error
	return regionList, err
}
